<?php
namespace app\mobile\controller;
use think\Controller;
use think\Controllers;
use think\Session;
use think\Cookie;
use think\Jump;
use think\Db;
class Person extends Controllers{
	public function index(){
		$member = Session::get("user");
		$sql = "select * from xuan_zy_article where uid = '{$member['uid']}' and uniacid = 2 and yidu = 0 and lanmu = '-6'";
		$xiaoxi=DB::query($sql);
	    $sql ="select * from ims_xuan_zy_person where uid = '{$member['uid']}'";
	    $user = DB::query($sql)[0];
	    $yewuy = $user['yewuy'];
	    $sum=DB::query("SELECT SUM(money) as sum FROM ims_xuan_zy_money  where uid='{$member['uid']}' and src_text='收入'")[0];
	    $sum['sum']=$sum['sum']?$sum['sum']:0.00;
	    $tixian=DB::query("SELECT SUM(money) as sum FROM  ims_xuan_zy_money where uid='{$member['uid']}' and src_text='提现'")[0];
	    $tixian['sum']=$tixian['sum']?$tixian['sum']:0.00;
	    $credit2=DB::query("SELECT credit2 FROM ims_mc_members where uid='{$member['uid']}'");
	}
	public function bank_sel(){
	    $bank_name = $_REQUEST['bank_name'];
	    $day = date("Ymd",time());
	    $yester = date("Ymd",time()-60*60*24);
	    $month = date("Ym",time());
	    $id = $me['uid'];
	    $sql = "select count(id) as count from ims_xuan_zy_bankorder where status = 3 and bank_name = '{$bank_name}' and suid = {$id} and  DATE_FORMAT(update_time,'%Y%m%d') = '{$day}'";
	    $data['day'] = DB::query($sql)[0]['count'];
	    $sql = "select count(id) as count from ims_xuan_zy_bankorder where status = 3 and  bank_name = '{$bank_name}' and suid = {$id} and DATE_FORMAT(update_time,'%Y%m%d') = '{$yester}'";
	    $data['yester']=  DB::query($sql)[0]['count'];
	    $sql = "select count(id) as count from ims_xuan_zy_bankorder where status = 3 and  bank_name = '{$bank_name}' and suid = {$id} and DATE_FORMAT(update_time,'%Y%m') = '{$month}'";
	    $data['month'] =  DB::query($sql)[0]['count'];
	    $sql = "select count(id) as count from ims_xuan_zy_bankorder where status = 3 and  bank_name = '{$bank_name}' and suid = {$id} ";
	   $data['all']=  DB::query($sql)[0]['count'];
	    echo json_encode($data);
	}
	public function team_count(){
			$me = Session::get("user");
		    if($me['yewuy'] != 1){
	        $data['day'] = 0;
	        $data['yester'] = 0;
	        $data['month'] = 0;
	        $data['all'] = 0;
	        echo json_encode($data);die;
	    	}
		     $bank_name = $_REQUEST['bank_name'];
		    
		    $day = date("Ymd",time());
		    $yester = date("Ymd",time()-60*60*24);
		    $month = date("Ym",time());
		    $id = $me['uid'];
		    $sql = "select m.uid from ims_xuan_zy_userup as p left join ims_xuan_zy_person as m  on p.uid = m.uid  where p.suid = {$id} and m.level != 0";
		    $us = DB::query($sql);
		    $user = "";
		    foreach ($us as $key => $value) {
		        $user.=$value['uid'].",";
		    }
		    $user = trim($user,",");
		    $sql = "select count(id) as count from ims_xuan_zy_bankorder where status = 3 and suid in ({$user}) and bank_name = '{$bank_name}' and  DATE_FORMAT(update_time,'%Y%m%d') = '{$day}'";
		    $data['day']  =isset(DB::query($sql)[0]['count'])?DB::query($sql)[0]['count']:0;
		    $sql = "select count(id) as count from ims_xuan_zy_bankorder where status = 3 and suid  in ({$user}) and bank_name = '{$bank_name}' and DATE_FORMAT(update_time,'%Y%m%d') = '{$yester}'";
		     $data['yester']  =isset(DB::query($sql)[0]['count'])?DB::query($sql)[0]['count']:0;
		    $sql = "select count(id) as count from ims_xuan_zy_bankorder where status = 3 and suid  in ({$user}) and bank_name = '{$bank_name}' and DATE_FORMAT(update_time,'%Y%m') = '{$month}'";
		    $data['month'] = isset(DB::query($sql)[0]['count'])?DB::query($sql)[0]['count']:0;
		    $sql = "select count(id) as count from ims_xuan_zy_bankorder where status = 3 and  suid  in ({$user}) and bank_name = '{$bank_name}'";
		    $data['all'] = isset(DB::query($sql)[0]['count'])?DB::query($sql)[0]['count']:0;
		    echo json_encode($data);die;
		}
	 public function sel(){
	 	 if ($me['yewuy'] != 1) {
        $data['error'] = 0;
         $data['order'] = 0;
         $data['true'] = 0;
         echo json_encode($data);die;
    }
	    $id = $me['uid'];
	    $sql = "select m.uid from ims_xuan_zy_userup as p left join ims_xuan_zy_person as m  on p.uid = m.uid  where p.suid = {$id} and m.level != 0";
	    $us = pdo_fetchall($sql);
	    $user = "";
	    foreach ($us as $key => $value) {
	        $user.=$value['uid'].",";
	    }
	    $user = trim($user,",");
	    $start = $_REQUEST['start'];
	    $end = $_REQUEST['end'];
	    $name = $_REQUEST['name'];
	    $bank = $_REQUEST['bank'];
	    $where = "where 1 = 1 ";
	    if($start){
	        $start = date("Ymd",strtotime($start));
	        $where .= " and DATE_FORMAT(update_time,'%Y%m%d')>='{$start}' ";
	    }
	    if($end){
	        $end = date("Ymd",strtotime($end));
	        $where .= " and DATE_FORMAT(update_time,'%Y%m%d')<='{$end}' ";
	    }
	    if ($name) {
	        $sql = "select m.uid,p.suid from ims_xuan_zy_person as m left join ims_xuan_zy_userup as p  on m.uid = p.uid  where m.name = '{$name}' and m.level != 0";
	        $uid = DB::query($sql)[0];
	        if($uid){
	            if ($me['uid'] == $uid['suid']) {
	                $where .= " and  suid = '{$uid['uid']}'";
	            } else {
	                $data['error'] = 1;
	                $data['data'] = "该用户不是您团队成员"; 
	                echo json_encode($data);die;
	            }

	        } else {
	            $data['error'] = 1;
	            $data['data'] = "用户不存在或不是代理";
	            echo json_encode($data);die;


	        }
	    } else {
	           $where .= " and  suid in ({$user})";
	 
	    }
	    if ($bank) {
	        $where .= "and bank_name = '{$bank}'";
	    }
	    $sql = "select count(id) as count from ims_xuan_zy_bankorder {$where}";
	    $data['error'] = 0;
	    $data['order'] = isset(DB::query($sql)[0]['count'])?DB::query($sql)[0]['count']:0;
	    $sql = "select count(id) as count from ims_xuan_zy_bankorder {$where} and status = 3";
	    $data['true'] =isset(DB::query($sql)[0]['count'])?DB::query($sql)[0]['count']:0;
	    echo json_encode($data);
	 }
	public function comein(){
		$member = Session::get("user");
	    $sum=DB::query("SELECT SUM(money) as sum FROM ims_xuan_zy_money where uid='{$member['uid']}' and src_text='收入'")[0];
	    $sum['sum']=$sum['sum']?$sum['sum']:0.00;
	    $tixian=DB::query("SELECT SUM(money) as sum FROM ims_xuan_zy_money where uid='{$member['uid']}' and src_text='提现'")[0];
	    $tixian['sum']=$tixian['sum']?$tixian['sum']:0.00;
	    $credit2=DB::query("SELECT credit2 FROM ims_mc_members where uid='{$member['uid']}' ")[0];

	    $type=!empty($_REQUEST['type'])?$_REQUEST['type']:'index';
	    $order=DB::query("select * from ims_xuan_zy_tixian where uid='{$member['uid']}' and uniacid=2")[0];
	    $order['info']= json_decode($order['content'],1);
    
	}
	public function kehu() {
	$member = Session::get("user");
		    //客户中心
    $wheres=" a.suid= '{$member['uid']}' and a.uniacid=2 and b.uid=a.uid and b.level>:level";


    $total1 = DB::query("select count(*) as count  from ims_xuan_zy_userup  as a INNER JOIN ims_xuan_zy_person as b WHERE   {$wheres}  order by a.id desc")[0]['count'];

    $wheres2=" a.suid='{$member['uid']}' and a.uniacid=2 and b.uid=a.uid and b.level=0";


    $total2 = DB::query("select count(*) as count from ims_xuan_zy_userup as a INNER JOIN ims_xuan_zy_person as b WHERE   {$wheres2}  order by a.id desc")[0]['count'];
	 echo json_encode(["total1"=>$total1,"total2"=>$total2]);
	}
}